package api

import (
	"api-gateway/pkg"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"os"
)

func UploadV1(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": err.Error(),
		})
		return
	}

	// 2. 保存到临时目录
	_ = os.MkdirAll("./tmp", os.ModePerm) // 确保 tmp 目录存在
	localPath := "./tmp/" + file.Filename
	if err := c.SaveUploadedFile(file, localPath); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to save file"})
		return
	}

	// 调用封装的 OSS 上传方法
	url, err := pkg.UploadV1(localPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": fmt.Sprintf("oss upload failed: %v", err),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"message": "upload success",
		"url":     url,
	})
}
